package com.meixiang.adapter.service;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.services.Evaluate;
import com.meixiang.entity.training.TrainingEvaluate;
import com.meixiang.util.AbDateUtil;
import com.meixiang.util.AbStrUtil;
import com.meixiang.util.GlideHelper;
import com.meixiang.view.EvaluateView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 服务评价适配器
 */
public class EvaluateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private final int EVALUATE_TYPE = 1;
    private final int CONTENT_TYPE = 2;
    private List<TrainingEvaluate> trainingEvaluates = new ArrayList<>();
    private int type = 1;//1:显示总体进度，0只显示评价列表
    private List<Evaluate> mEvaluates = new ArrayList<>();

    public EvaluateAdapter() {
    }

    public EvaluateAdapter(int type) {
        this.type = type;

    }

    @Override
    public int getItemViewType(int position) {
        if (type == 0) {
            return position;
        } else {
            if (position == 0) {
                return EVALUATE_TYPE;
            } else {
                return CONTENT_TYPE;
            }
        }
    }

    public void addAll(List<TrainingEvaluate> trainingEvaluates) {
        this.trainingEvaluates.addAll(trainingEvaluates);
        this.notifyDataSetChanged();
    }

    public void addEvaluateData(List<Evaluate> mEvaluates) {
        this.mEvaluates.clear();
        /*for (int i = 0; i < mEvaluates.size(); i++) {
            if (i == 0) {
                mEvaluates.get(i).setTagName("服务态度");
            } else if (i == 1) {
                mEvaluates.get(i).setTagName("技师手艺");
            } else if (i == 2) {
                mEvaluates.get(i).setTagName("用着舒服");
            } else if (i == 3) {
                mEvaluates.get(i).setTagName("包装很好");
            }
            if (i < 4) {
                this.mEvaluates.add(mEvaluates.get(i));
            }
        }*/
        this.mEvaluates.addAll(mEvaluates);
        notifyItemChanged(0);
    }

    public void clear() {
        this.trainingEvaluates.clear();
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view;
        if (type == 1 && viewType == EVALUATE_TYPE) {
            view = LayoutInflater.from(mContext).inflate(R.layout.view_evaluation, parent, false);
            return new EvaluateViewHolder(view);
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.list_item_evaluate, parent, false);
            return new ViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (type==1){
            if (position == 0 ) {
                EvaluateViewHolder evaluate = (EvaluateViewHolder) holder;
                evaluate.vEvaluate.setData(mEvaluates);
                return;
            }else{
                position--;
            }

        }
        TrainingEvaluate trainingEvaluate = trainingEvaluates.get(position);
        EvaluateAdapter.ViewHolder viewHolder = (ViewHolder) holder;
        GlideHelper.showImage(mContext, trainingEvaluate.getMemberImg(), viewHolder.ivHeadPortrait);
        viewHolder.tvName.setText(trainingEvaluate.getMemberName());
        if (!AbStrUtil.isEmpty(trainingEvaluate.getEvaluateTime())) {
            viewHolder.tvTime.setText(AbDateUtil.formatDateTime(trainingEvaluate.getEvaluateTime()));
        } else {
            viewHolder.tvTime.setText(trainingEvaluate.getEvaluateTime());
        }

        viewHolder.tvContent.setText(trainingEvaluate.getContent());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        viewHolder.rvPhoto.setLayoutManager(linearLayoutManager);
        CosmetologistListAdapter cosmetologistListAdapter = new CosmetologistListAdapter(mContext);
        viewHolder.rvPhoto.setAdapter(cosmetologistListAdapter);
        if (trainingEvaluate.getEvalPicLinks() != null && trainingEvaluate.getEvalPicLinks().size() > 0) {
            cosmetologistListAdapter.addUrls(trainingEvaluate.getEvalPicLinks());
        }


    }

    @Override
    public int getItemCount() {
        return type==1?trainingEvaluates.size()+1:trainingEvaluates.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_head_portrait)
        ImageView ivHeadPortrait;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.rv_photo)
        RecyclerView rvPhoto;
        @Bind(R.id.evaluate_tv_content)
        TextView tvContent;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class EvaluateViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.v_evaluate)
        EvaluateView vEvaluate;

        EvaluateViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
